import java.util.List;

public class RemoveDuplicatesFromSortedList {
    /**
     * 存在一个按升序排列的链表，给你这个链表的头节点 head ，请你删除所有重复的元素，使每个元素 只出现一次 。
     * 返回同样按升序排列的结果链表。
     * @param head
     * @return
     */
    public static ListNode deleteDuplicates(ListNode head) {
        if (head==null)
            return null;
        ListNode head_result = head;
        int tmp_val = head.val;
        ListNode tmp_node = head;
        while (head!=null){
            if (head.val==tmp_val){
                tmp_node.next = head.next;
            }else {
                tmp_val = head.val;
                tmp_node = head;
            }
            head = head.next;
        }
        return head_result;

    }
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;
        ListNode result = deleteDuplicates(head);
        while (result!=null){
            System.out.println(result.val);
            result = result.next;
        }




    }
}
